//
//  HFTableViewFirst.m
//  ThridPackage
//
//  Created by 韩艳锋 on 2019/7/23.
//  Copyright © 2019 韩艳锋. All rights reserved.
//

#import "HFTableViewFirst.h"
#import "HFRow.h"

@interface HFTableViewFirst ()
<HFRowDelegate>

@property (copy, nonatomic) NSArray<HFRow*> * sourceArray;

@end

@implementation HFTableViewFirst

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.dataSource = self;
        self.delegate = self;
    }
    return self;
}


- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    HFRow * row = self.sourceArray[indexPath.row];
    row.delegate = self;
    return [row cellWithTableView:tableView];
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sourceArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.actionBack(self.sourceArray[indexPath.row], -1, indexPath, nil);
}

- (void)actionFromCellRow:(HFRow *)viewData tag:(NSInteger)tag value:(id)value {
    NSIndexPath * indexPath = nil;
    NSArray * array = [self.sourceArray copy];
    for (int i = 0; i != array.count; i++) {
        if ([array[i] isEqual:viewData]) {
            indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            break;
        }
    }
    self.actionBack(viewData, tag, indexPath, value);
}

- (void)setSource:(NSArray<HFRow*> *)sourceArray {
    self.sourceArray = sourceArray;
    [self reloadData];
}
@end
